<?php


namespace app\api\controller;


use app\BaseController;

//引入模型
use app\models\system\Lecturer as LecturerModel;

use app\Request;
use broadcast\services\UtilService as Util;
//处理redis
use broadcast\redis\setRedis;
use broadcast\redis\RedisRecord;
use app\models\system\Curriculum;
/**
 * Class Index
 * @package app\api\controller
 */
class Lecturer extends BaseController
{
    //获取讲师列表
    public function lst(Request $request) 
    {
        session("website",$request->website());

        $low_Lecturer = setRedis::getCache(session('website'),"LecturerList");
        if ($low_Lecturer) {
            $lecturer = $low_Lecturer;
        }else {
            
            $lecturer = LecturerModel::where("state",1)->order("rank desc")->select();
            if ($lecturer) {
                $lecturer = $lecturer->toArray();
                setRedis::setCache(session('website'),"LecturerList",$lecturer,0);
            }
            
        }



        
        $lecturerList = array();

        
        foreach ($lecturer as &$item) {

            $LecturerGiveRecord = new RedisRecord(session("website"),'LecturerGiveRecord:'.$item['id']);
            $valueList = $LecturerGiveRecord->getLecturers();
            $item['thumbs_total'] = $valueList['total']+$item['thumbs_dault'];
            $item['thumbs_month'] = $valueList['month']+$item['thumbs_dault'];
            $item['thumbs_week'] = $valueList['week']+$item['thumbs_dault'];
            $item['thumbs_day'] = $valueList['day']+$item['thumbs_dault'];



            $LecturerGiveRecord = new RedisRecord(session("website"),'LecturerVoteRecord:'.$item['id']);
            $valueList = $LecturerGiveRecord->getLecturers();
            $item['votes_total'] = $valueList['total']+$item['votes_dault'];
            $item['votes_month'] = $valueList['month']+$item['votes_dault'];
            $item['votes_week'] = $valueList['week']+$item['votes_dault'];
            $item['votes_day'] = $valueList['day']+$item['votes_dault'];
            $item['content']=tohtml($item['content']);
            unset($item['website']);
            
            unset($item['create_time']);
            unset($item['state']);
            unset($item['rank']);
            $lecturerList[] = $item;
        }


        $ranks = ['total','day','week','month'];
        $thumbsRanksList = array();
        $votesRanksList = array();
        foreach ($ranks as $item) {

            //点赞
            $lecturerValue = array_column($lecturerList,'thumbs_'.$item);
            array_multisort($lecturerValue,SORT_DESC,$lecturerList);
            $thumbsRanksList[$item] = $lecturerList;

            //投票
            $lecturerValue = array_column($lecturerList,'votes_'.$item);
            array_multisort($lecturerValue,SORT_DESC,$lecturerList);
            $votesRanksList[$item] = $lecturerList;

        }
        
        return app("json")->success(['thumbList'=>$thumbsRanksList,'voteList'=>$votesRanksList]);
    }

    //讲师点赞
    public function give(Request $request) 
    {
        $data = Util::postMore([
            ['id','']
        ]);
        
        $uid = $request->uid();
        if (strpos($uid, 'yk') !== false) return app("json")->success('游客状态无法签到');


        session("website",$request->website());
        $nowTime = time();
        $key = "Lecturcer:give:".$uid;

        $give = setRedis::getCache(session("website"),$key);
        if ($give) {
            if (((int)$give['time'] + (4 * 60 * 60)) > $nowTime) {
                return app("json")->success('已经点赞过了');
            }else {

                $res = LecturerModel::where("id",$data['id'])->inc('thumbs')->save();
                $give = ['time' => $nowTime, 'num' => $give['num'] + 1];
                
                $low_Lecturer = setRedis::getCache(session('website'),"LecturerList");

                foreach ($low_Lecturer as &$value) {
                    if ($value['id'] == $data['id']) {
                        $value['thumbs'] = (int)$value['thumbs'] + 1;
                    }
                }
                setRedis::setCache(session('website'),"LecturerList",$low_Lecturer,0);


                $LecturerGiveRecord = new RedisRecord(session("website"),'LecturerGiveRecord:'.$data['id']);
                $LecturerGiveRecord->addLecturer();
                setRedis::setCache(session("website"),$key,$give);
            }

        }else{
            $res = LecturerModel::where("id",$data['id'])->inc('thumbs')->save();
            $give = ['time' => $nowTime, 'num' => 1];
            $low_Lecturer = setRedis::getCache(session('website'),"LecturerList");

            foreach ($low_Lecturer as &$value) {
                if ($value['id'] == $data['id']) {
                    $value['thumbs'] = (int)$value['thumbs'] + 1;
                }
            }
            setRedis::setCache(session('website'),"LecturerList",$low_Lecturer,0);
            
            $LecturerGiveRecord = new RedisRecord(session("website"),'LecturerGiveRecord:'.$data['id']);
            $LecturerGiveRecord->addLecturer();
            setRedis::setCache(session("website"),$key,$give);
        }
        
        



        return $res ? app("json")->success('点赞成功') : app("json")->fail('点赞失败');
    }

    //讲师投票
    public function getVote(Request $request) 
    {
        $data = Util::postMore([
            ['id','']
        ]);
        $uid = $request->uid();
        if (strpos($uid, 'yk') !== false) return app("json")->success('游客状态无法签到');
        session("website",$request->website());
        $nowTime = time();

        $key = "Lecturcer:vite:".$uid;
        $vote = setRedis::getCache(session("website"),$key);

        if ($vote) {
            if (((int)$vote['time'] + (4 * 60 * 60)) > $nowTime) {
                return app("json")->success('已经投票过了');
            }else {
                $LecturerVoteRecord = new RedisRecord(session("website"),'LecturerVoteRecord:'.$data['id']);
                $LecturerVoteRecord->addLecturer();
                $res = LecturerModel::where("id",$data['id'])->inc('votes')->save();
                $vote = ['time' => $nowTime, 'num' => $vote['num'] + 1];
                $low_Lecturer = setRedis::getCache(session('website'),"LecturerList");

                foreach ($low_Lecturer as &$value) {
                    if ($value['id'] == $data['id']) {
                        $value['votes'] = (int)$value['votes'] + 1;
                    }
                }
                setRedis::setCache(session('website'),"LecturerList",$low_Lecturer,0);
                setRedis::setCache(session("website"),$key,$vote);
                
            }

        }else{
            $vote = ['time' => $nowTime, 'num' => 1];
            $res = LecturerModel::where("id",$data['id'])->inc('votes')->save();
            setRedis::setCache(session("website"),$key,$vote);

            $low_Lecturer = setRedis::getCache(session('website'),"LecturerList");

                foreach ($low_Lecturer as &$value) {
                    if ($value['id'] == $data['id']) {
                        $value['votes'] = (int)$value['votes'] + 1;
                    }
                }
                setRedis::setCache(session('website'),"LecturerList",$low_Lecturer,0);
            $LecturerVoteRecord = new RedisRecord(session("website"),'LecturerVoteRecord:'.$data['id']);
            $LecturerVoteRecord->addLecturer();
        }

        
        return $res ? app("json")->success('投票成功') : app("json")->fail('投票失败');
    }


    //判断当前是那个讲师讲课
    public function currentLecture (Request $request) 
    {
        session("website",$request->website());
        //获取课程表列表
        $low_week = setRedis::getCache(session("website"),"CurriculumList");

        if ($low_week) {
            $week = $low_week;
        } else {

            $week = Curriculum::order("start_time asc")->select();

            if ($week) {
                $week = $week->toArray();
                setRedis::setCache(session("website"),"CurriculumList",$week,0);
            }
        }
        $weekList = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday'];
        //判断当前是那个讲师
        $nowTime = time();
        $nowLecture = array();
        foreach ($week as $item) {

            if ($nowTime >= strtotime(date("Y-m-d").' '.$item['start_time']) && $nowTime <= strtotime(date("Y-m-d").' '.$item['end_time'])) {
                $nowLecture = json_decode($item[$weekList[date("w")]],true);
            }

        }
        if (empty($nowLecture)) {

            $nowLecture = [
                'content' => '',
                'name' => '暂无讲师',
                'mark' => ''
            ];

        }else{

            if ($nowLecture['name'] == '' ) { 
                $nowLecture = [
                    'content' => '',
                    'name' => '暂无讲师',
                    'mark' => ''
                ];
            }
        }

        return app("json")->success($nowLecture); 
    }

}